//============================================
// Define VDD and VSS
//============================================
VVDD (VDD 0) vsource dc=pvdd
VVSS (VSS 0) vsource dc=0

//============================================
// Half cell for SNM test
//============================================
subckt HALFCELL (in out BL WL VDD VBP VSS VBN)
parameters vt0=0 vt1=0 vt2=0
MN (out G0 VSS VSS) NMOS_VTL w=270n l=50n 
MP (out G1 VDD VDD) PMOS_VTL w=270n l=50n 

MT (BL G2 out VSS ) NMOS_VTL w=330n l=50n 
 
V0 (in G0) vsource dc=vt0 type=dc
V1 (in G1) vsource dc=vt1 type=dc
V2 (WL G2) vsource dc=vt2 type=dc
ends HALFCELL

//============================================
// 6T Bitcell
//============================================
subckt BITCELL (in1h out1h in2h out2h BL BLB WL VDD  VSS)
parameters vt0=0 vt1=0 vt2=0 vt3=0 vt4=0 vt5=0
ICellAh (in1h out1h BL WL VDD VDD VSS VSS) HALFCELL vt0=pvt0 vt1=pvt1 vt2=pvt2
ICellBh (in2h out2h BLB WL VDD VDD VSS VSS) HALFCELL vt3=pvt3 vt4=pvt4 vt5=pvt5
ends BITCELL
//====================================================
// Subcircuit to measure SNM value
//====================================================
subckt SNMmeas (U INV1in INV1out INV2in INV2out v1mv2)
  e1 (1 0 U 0) vcvs gain=1/sqrt(2)
  e2 (INV1in 1 v1 0) vcvs gain=1/sqrt(2)
  ev1 (v1 U INV1out 0) vcvs gain=sqrt(2)
  Rdummy  (v1 0)  resistor r=100M
  Rdummy2 (v2 0) resistor r=100M
  Rdummy3 (v1mv2 0) resistor r=100M

  e3 (3 0 U 0) vcvs gain=-1/sqrt(2)
  e4 (INV2in 3 v2 0) vcvs gain=1/sqrt(2)
  enu (nu 0 U 0) vcvs gain=-1
  ev2 (v2 nu INV2out 0) vcvs gain=sqrt(2)
  ediff1 (negv2 0 v2 0) vcvs gain=-1
  ediff2 (v1mv2 negv2 v1 0) vcvs gain=1
ends SNMmeas

//============================================
// Test Read SNM
//============================================
//ICellAh (in1h out1h BL WL VDD VDD VSS VSS) HALFCELL (vt0=pvt0 vt1=pvt1 vt2=pvt2)
//ICellBh (in2h out2h BLB WL VDD VDD VSS VSS) HALFCELL (vt3=pvt3 vt4=pvt4 vt5=pvt5)


ICell0 (in1h out1h in2h out2h BL BLB WL VDD VSS) BITCELL vt0=pvt0 vt1=pvt1 vt2=pvt2 vt3=pvt3 vt4=pvt4 vt5=pvt5

Isnmh (U in1h out1h in2h out2h v1mv2h) SNMmeas

VU (U 0) vsource dc=pvin
VWL (WL 0) vsource dc=pvdd
VBL (BL 0) vsource dc=pvdd
VBLB (BLB 0) vsource dc=pvdd
